import RobotError from "../model/errors/robot-error.js";
import { Fighter } from "../model/player/ability.js";



export default class Fight {
	fighter1: Fighter;
	fighter2: Fighter;

	constructor(fighter1: Fighter, fighter2: Fighter) {
		this.fighter1 = fighter1;
		this.fighter2 = fighter2;
	}

	/**
	 * 决斗，有玩家死亡即结束
	 * @returns 回合记录
	 */
	duel() {
		//存在决斗玩家死亡
		if (!this.fighter1.isAlive()) throw new RobotError(`你人都死了还决斗个毛线`);
		if (!this.fighter2.isAlive()) throw new RobotError(`你是想鞭尸吗`);
		//双方为同一人
		if (this.fighter1 == this.fighter2) throw new RobotError(`决斗自己是吧`);

		let isF1Hit = true;
		let records = [];
		while (this.fighter1.isAlive() && this.fighter2.isAlive()) {
			records.push(this.fight(isF1Hit));
			isF1Hit = !isF1Hit;

		}

		return {
			msg: `决斗完成`,
			winner: isF1Hit ? this.fighter2 : this.fighter1,
			records: records,
			success: true
		};
	}

	haveDeath() {
		return this.fighter1.currentHp <= 0 || this.fighter2.currentHp <= 0;
	}

	fight(isF1Hit: boolean) {
		// let result = {
		// 	damage: damage,
		// 	msg: `【${attacker.nick}】❤ ${attacker.currentHp}/${attacker.maxHp}\n` +
		// 		`  ↓👊  ${damage} ❤\n` +
		// 		`【${this.nick}】❤ ${this.currentHp}/${this.maxHp}`
		// };
		let attacker = this.fighter2;
		let target = attacker;
		if (isF1Hit) attacker = this.fighter1;
		else target = this.fighter1;
		let status = attacker.hit(target);

		if (status.success) return {
			status: status,
			msg: `【${attacker.name}】❤ ${attacker.currentHp}/${attacker.maxHp}\n` +
				`  ↓👊  ${status.damage} ❤\n` +
				`【${target.name}】❤ ${target.currentHp}/${target.maxHp}`
		};
		if (status.dodgeSuccess) return {
			status: status,
			msg: `【${attacker.name}】❤ ${attacker.currentHp}/${attacker.maxHp}\n` +
				`  ↓👊  ${status.damage} ❤ （闪避）\n` +
				`【${target.name}】❤ ${target.currentHp}/${target.maxHp}`
		};
	}
}